package com.laputa.service.method.impl;

import com.laputa.model.City;
import com.laputa.model.Ground;
import com.laputa.model.manager.CityManager;
import com.laputa.model.manager.GroundManager;
import com.laputa.model.manager.UserManager;
import com.laputa.service.method.BuildService;
import com.laputa.service.model.BuildData;
import com.laputa.service.model.MainFrameData;
import com.laputa.service.model.ShowGroundData;
import com.laputa.system.Lib;
import com.laputa.system.element.BuildingElement;
import com.laputa.system.strategy.BuildingConstructor;

public class BuildServiceImpl implements BuildService {

	private BuildData buildData;
	private BuildingConstructor buildingConstructor;
	private UserManager userManager;
	private GroundManager groundManager;
	private CityManager cityManager;
	private MainFrameData mainFrameData;
	private ShowGroundData showGroundData;
	
	public void setBuildData(BuildData buildData){
		this.buildData = buildData;
	}
	
	public void setBuildingConstructor(BuildingConstructor buildingConstructor){
		this.buildingConstructor = buildingConstructor;
	}
	
	public void setUserManager(UserManager userManager){
		this.userManager = userManager;
	}
	
	public void setGroundManager(GroundManager groundManager){
		this.groundManager = groundManager;
	}
	
	public void setCityManager(CityManager cityManager){
		this.cityManager = cityManager;
	}
	
	public void setMainFrameData(MainFrameData mainFrameData){
		this.mainFrameData = mainFrameData;
	}
	
	public void setShowGroundData(ShowGroundData showGroundData){
		this.showGroundData = showGroundData;
	}
	
	public String build(String type, String groundID, String price, String userID) {
		Long money = userManager.getMoney(userID);
		if(money < Long.parseLong(price))
			return "您的金钱不足！";
		else{
			BuildingElement building = this.buildingConstructor.construct(Long.parseLong(type));
			long groundprice = calcPrice(building);
			int buildtime = Lib.getCurrentTime();
			groundManager.save(groundID, building, groundprice, buildtime);
			return Lib.getBuilding(Long.parseLong(type))+
				   					"建造成功！\n高度:\t"+building.getTotalLayer()+
				   					"层\n人口：\t"+building.getPopulation();
		}
	}
	
	private long calcPrice(BuildingElement building){
		long pop = building.getPopulation();
		long layer = building.getTotalLayer();
		long type = building.getType();
		long res = Lib.emptyGroundPrice + (pop+layer+type)*1000;
		return res;
	}
	
	public MainFrameData apply(String price, String userID){
		Long money = userManager.getMoney(userID);
		if(money < Long.parseLong(price))
			return null;
		else{
			Long remain = money - Long.parseLong(price);
			userManager.saveMoney(userID, remain.toString());
			Long id =  Long.parseLong(userID);
			City city = cityManager.getCityObject(userManager.getUser(id.toString()).getCity().toString(), id.toString());
			com.laputa.dao.model.User user = userManager.getUser(userID); 
			String name = user.getName();
			this.mainFrameData.setId(id);
			this.mainFrameData.setName(name);
			this.mainFrameData.setCity(city);
			this.mainFrameData.setMoney(user.getMoney());
			return this.mainFrameData;
		}
	}
	
	public BuildData loadBuilding() {
		return this.buildData;
	}
	
	public ShowGroundData getShowGroundData(String groundID) {
		Ground ground = groundManager.getGroundObject(groundID);
		this.showGroundData.setGround(ground);
		return this.showGroundData;
	}

}
